<template>
	<div v-loading="loading">
		<div class="content-top">
			<span class="h1">系统管理</span>
			<span class="h2">系统管理</span>
		</div>
		<div class="con">
			<div class="search clearfix" style="margin-top: 10px;">
				<el-button type="primary" @click="addust()">新建用户</el-button>
				<div class="right-search1">
					<el-input v-model="input1" placeholder="请输入..."></el-input>
					<el-button type="primary" @click="look()" style="margin-left: 10px;">查看</el-button>
				</div>
			</div>
			<div style="margin-top: 10px;padding-bottom: 100px;overflow: auto">
				<el-table ref="tableData" :data="tableData" highlight-current-row style="width: 100%">
					<el-table-column type="index" width="60">
					</el-table-column>
					<el-table-column prop="Login" label="用户名">
					</el-table-column>
					<el-table-column prop="CNName" label="姓名">
					</el-table-column>
					<el-table-column prop="Tel" label="电话">
					</el-table-column>
					<el-table-column prop="Status" label="状态">
					</el-table-column>
					<el-table-column prop="address" label="操作" width="230">
						<template slot-scope="scope">
							<el-dropdown trigger="click">
								<span class="el-dropdown-link"><el-button type="primary">操作<i class="el-icon-arrow-down el-icon--right"></i></el-button></span>
								<el-dropdown-menu slot="dropdown" class="div">
									<el-dropdown-item>
										<el-button size="small" type="primary" @click="modifys(scope.row.ID,scope.row.CNName)">修改</el-button>
									</el-dropdown-item>
									<el-dropdown-item>
										<el-button size="small"  type="danger" @click="deleteRow(scope.row.ID)">删除</el-button>
									</el-dropdown-item>
									<el-dropdown-item>
										<el-button size="small"  type="info" @click="reset(scope.row.Login)">重置</el-button>
									</el-dropdown-item>
								</el-dropdown-menu>
							</el-dropdown>
						</template>
					</el-table-column>
				</el-table>
				<div class="pages">
					<el-pagination small :current-page="index" @current-change="handleCurrentChange" layout="prev, pager, next" :total="total" background prev-text='上一页' next-text='下一页'>
					</el-pagination>
				</div>
			</div>
		</div>
		
		<!--新建操作员-->
		<el-dialog title="用户管理" :visible.sync="dialogVisible" :before-close="handleClose">
			<el-form ref="form" :model="form" label-width="80px">
				<el-form-item label="用户名">
					<el-input v-model="form.name"></el-input>
				</el-form-item>
				<el-form-item label="姓名">
					<el-input v-model="form.username"></el-input>
				</el-form-item>
				<el-form-item label="密码">
					<el-input v-model="form.pass" type="password"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="quxia()">取 消</el-button>
				<el-button type="primary" @click="newUere()">确 定</el-button>
			</span>
		</el-dialog>
		<!--修改操作员-->
		<el-dialog title="修改操作员" :visible.sync="mark5" width="30%" :before-close="handleClose">
			<el-form ref="form" :model="form" label-width="80px">
				<el-form-item label="姓名">
					<el-input v-model="form.username"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="quxia()">取 消</el-button>
				<el-button type="primary" @click="updateuser()">确 定</el-button>
			</span>
		</el-dialog>
		<!--修改操作员-->
		<el-dialog title="密码重置" :visible.sync="mark1" width="30%" :before-close="handleClose">
			<el-form ref="form" :model="form" label-width="80px">
				<el-form-item label="新密码">
					<el-input v-model="form.pass" type="password"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="quxia()">取 消</el-button>
				<el-button type="primary" @click="resetfn()">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import apis from '../../../api/prouserApi'
	export default {
		name: 'sys',
		data() {
			return {
				mark5:false,//修改用户
				loading:false,
				Page1:1, 
 				pagesize:10, 
				input1: "",
				leng:1,
				form: {
					name: '',
					tel: '',
					username: "",
					pass:'',
					paw:''
				},
				tableData: [],//用户列表
				currentRow: null,
				dialogVisible: false,
				index:1,
				ProviderID:'',
				id:'',//商家id
				mark1:false,
				total:1,
			}
		},
		created() {
			this.ProviderID = sessionStorage.ProviderID
			this.userlist()
		},
		updated: function() {
	
		},
		methods: {
			//分页
			handleCurrentChange(val){
				this.index = val
				this.loading = true
				this.$axios.post(apis.userlist,{
				  "Para": this.input1,
				  "PageSize": 10,
				  "PageIndex": val-1,
				  "OrderbyString": "Login"
				}).then(res=>{
					if(res.data.isOK){
						this.tableData = res.data.data.Data
						this.total = res.data.data.Items?res.data.data.Items:1
						this.loading = false
					}else{
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}	
				})
			},
			look(){
				this.index = 1
				this.userlist()
			},
			//新建用户弹框
			addust(){
				this.form.name=""
				this.form.username=""
				this.form.pass=""
				this.dialogVisible = true
			},
			//确定新建用户
			newUere(){
				this.loading = true
				this.$axios.post(apis.adduser,{
				  "ProviderID": this.ProviderID,
				  "Login": this.form.name,
				  "CNName": this.form.username,
				  "PWD": md5(this.form.pass),
				  "Status": '',
				}).then(res=>{
					if(res.data.isOK){
						this.userlist()
						this.dialogVisible = false
						this.loading = false
					}else{
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}	
				})
			},
			handleClose(){
				this.quxia()
			},
			quxia(){
				this.dialogVisible=false
				this.mark5 = false
				this.mark1 = false
			},
			//删除用户
			deleteRow(id){
				this.loading = true
				this.$axios.get(apis.deluser+'/'+id).then(res=>{
					if(res.data.isOK){
						this.userlist()
						this.loading = false
					}else{
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}	
				})
			},
			//修改用户
			modifys(id,name){
				this.id = id
				this.form.username = name,
//				this.getmodel(this.id)
				this.mark5 = true
			},
			//获取实体
			getmodel(id){
				this.$axios.get(apis.getmodel+'/'+id).then(res=>{
					if(res.data.isOK){
						this.form.username = res.data.data.CNName
					}else{
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.$message.error(res.data.msg);
						}
					}	
				})
			},
			//确定修改
			updateuser(){
				this.loading = true
				this.$axios.post(apis.edituser,{
				  "ID": this.id,
				  "ProviderID": this.ProviderID,
				  "CNName": this.form.username
				}).then(res=>{
					if(res.data.isOK){
						this.loading = false
						this.userlist()
						this.mark5 = false
					}else{
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.$message.error(res.data.msg);
						}
					}	
				})
			},
			//密码重置
			reset(login){
				this.login = login
				this.mark1 = true
			},
			//确认重置
			resetfn(){
				this.loading = true
				this.$axios.post(apis.editpwd,{
				  "Login": this.login,
				  "ProviderID": this.ProviderID,
				  "NewPwd": md5(this.form.pass),
				  "OldPwd": md5(this.form.pwd),
				  "ValidOldPwd": false
				}).then(res=>{
					if(res.data.isOK){
						this.loading = false
						this.mark1 = false
						this.userlist()
					}else{
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}	
				})
			},
			//获取列表
			userlist(){
				this.loading = true
				this.$axios.post(apis.userlist,{
				  "Para": this.input1,
				  "PageSize": 10,
				  "PageIndex": this.index-1,
				  "OrderbyString": "Login"
				}).then(res=>{
					if(res.data.isOK){
						this.tableData = res.data.data.Data
						this.total = res.data.data.Items?res.data.data.Items:1
						this.loading = false
					}else{
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}	
				})
			}
		}
	}
</script>

<style scoped>
	
	.con {
		width: 100%;
		box-sizing: border-box;
	}
	
	.search {
		height: auto;
		width: 100%;
		/*border: 1px solid #000;*/
	}
	
	.search button {
		margin: 10px;
	}
	
	.page {
		float: right;
		margin-top: 20px;
	}
	
	.el-dropdown-menu__item {
		text-align: center;
	}
	
	.el-dropdown-menu {
		min-width: 79px !important;
	}


</style>